package com.chima.action;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.SonPost;
import com.chima.service.SonPostService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import flexjson.JSONSerializer;

public class SonPostAction extends ActionSupport implements ModelDriven<SonPost> {
	
	SonPost sonPost=new SonPost();
	public SonPost getModel() {
	
		return sonPost;
	}
	private int start;// 开始页码
	private int limit; //每页显示
	private int page;//页数
	private boolean success=false ;
	private List<SonPost> sonPosts;
	private int id;
	private int depart;
	private int param;
	private SonPostService sonPostService;

	public SonPostService getSonPostService() {
		return sonPostService;
	}

	public void setSonPostService(SonPostService sonPostService) {
		this.sonPostService = sonPostService;
	}
	public int getParam() {
		return param;
	}

	public void setParam(int param) {
		this.param = param;
	}

	public int getDepart() {
		return depart;
	}

	public void setDepart(int depart) {
		this.depart = depart;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public List<SonPost> getSonPosts() {
		return sonPosts;
	}

	public void setSonPosts(List<SonPost> sonPosts) {
		this.sonPosts = sonPosts;
	}

	
	public String ListSonPost() throws Exception{
	    PageRequestBean pageRequestBean = new PageRequestBean();
	    DetachedCriteria criteria = DetachedCriteria.forClass(SonPost.class);
	    if(depart!=0){ 
			criteria.add(Restrictions.eq("department.id",depart));
		}
		pageRequestBean.setCriteria(criteria);
		pageRequestBean.setStart(start);
		pageRequestBean.setDept(depart);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean = sonPostService.findPost(pageRequestBean);
		sonPosts=pageResponseBean.getRows();
//        int total=0;
       int  total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.deepSerialize(sonPosts);  
        String resultJson2 =jsonSerializer.serialize(total);
        String jsonString = "{total:"+resultJson2.toString()+",sonposts:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
  }
	public void addSonPost() throws Exception{
		
		SonPost s=new SonPost();
		s.setName(sonPost.getName());
		s.setPost(sonPost.getPost());
		s.setDepartment(sonPost.getDepartment());
		s.setCoffe(sonPost.getCoffe());
		s.setAdcontent(sonPost.getAdcontent());
		sonPostService.savePost(s);
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
	public void deleteSonPost() throws Exception{
			sonPostService.deletePost(sonPost.getId());
		}
		
	public void editSonPost() throws Exception{
		SonPost s=new SonPost();
		s.setName(sonPost.getName());
		s.setPost(sonPost.getPost());
		s.setDepartment(sonPost.getDepartment());
		s.setCoffe(sonPost.getCoffe());
		s.setAdcontent(sonPost.getAdcontent());
		s.setId(sonPost.getId());
		sonPostService.updatePost(s);
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
	
	public String Listpost() throws Exception{
	    
		List<SonPost> sonPosts=sonPostService.findPostByPId(param);
		// 输出json内容到客户端
		int total=sonPosts.size();
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.deepSerialize(sonPosts);  
        String resultJson2 =jsonSerializer.serialize(total);
        String jsonString = "{total:"+resultJson2.toString()+",sonposts:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
  }
	
}

	


